package com.lankystar.logistucs.spring.dao.impl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.lankystar.logistucs.spring.dao.ITruckDao;
import com.lankystar.logistucs.spring.entity.Truck;

@Repository
public class TruckDaoImpl extends BaseDaoImpl implements ITruckDao {

	@Autowired
	private SessionFactory sf;
	
	public Session getSession(){
		System.out.println("session oneOrderDaoImpl");
		return sf.getCurrentSession();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> selectTruckAndStatuslist(int userId) {
		String sql = "from User u join u.company cp join cp.trucks t join t.truckStatus ts join t.truckType tt where u.userId = ?";
		List<Object[]> trucklist = getSession().createQuery(sql).setInteger(0, userId).list();
		return trucklist;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> selectCanUserTrucklist(Integer userId) {
		String sql = "";
		if(userId!=null){
			sql = "from User u join u.company cp "
					+ "join cp.trucks t join t.truckStatus ts join t.truckType tt  where u.userId = "+userId+" and ts.truckStatusKey = 1";
		}else{
			sql = "from Truck t join t.truckStatus ts join t.truckType tt where ts.truckStatusKey = 1";
		}
		List<Object[]>  trucklist = getSession().createQuery(sql).list();
		return trucklist;
	}

	@Override
	public Truck selectTruckById(int truckId) {
		Truck truck = (Truck) getSession().get(Truck.class, truckId);
		return truck;
	}

	@Override
	public Object[] selectTruckByuserId(int userId) {
		String hql = "from TwoOrder two join two.truck t join two.user u where u.userId = ? and two.remove = "+false+"";
		Object[] ob = (Object[]) getSession().createQuery(hql).setInteger(0,userId).uniqueResult(); 
		return ob;
	}
}
